package com.sos.core.game
{
	import com.sos.core.DataObject;
	import com.sos.logging.Logger;
	
	public class GameTurns extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		// JSON data properties names
		// public static const 	JSON_PROPERTY_ID:String					= "instanceId";
		
		// Default values
		// private static const 	DEFAULT_ID:int							= 0;
		
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _turns:Vector.<GameTurn>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameTurns(data:Object = null)
		{
			// Init object
			_turns = new Vector.<GameTurn>();
			//
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get turns():Vector.<GameTurn>
		{
			return _turns;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		/*
		protected function getTurnIndex(turn:GameTurn):int
		{
			for (var i:int = 0; i<_turns.length; i++)
			{
				if (_turns[i].equals(turn)) return i;
			}
			return -1;
		}
		*/
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			Logger.notImplemented("GameTurns", "validate");
		}
		
		override protected function fill(data:Object):void
		{
			Logger.notImplemented("GameTurns", "fill");
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 *  
		 * @param instanceId
		 * @return 
		 */
		/*
		public function contains(instanceId:uint):Boolean
		{
			return getTurnIndex(instanceId) > -1;
		}
		*/
		
		/**
		 *  
		 * @param instanceId
		 * @return 
		 */
		/*
		public function find(instanceId:uint):GameTurn
		{
			var turn:GameTurn;
			var index:int = getTurnIndex(instanceId);
			if (index > -1) turn = _turns[index];
			return turn;
		}
		
		
		public function equals(turns:GameTurns):Boolean
		{
			// Check turns number
			if (_turns.length != turns.turns.length) return false;
			// Check each card
			for (var i:int = 0; i < _turns.length; i++) if (!_turns[i].equals(turns.turns[i])) return false;
			// Equal
			return true;
		}
		
		public function copy(gameTurns:GameTurns):void
		{
			// Temporary list
			var turns:Vector.<GameTurn> = new Vector.<GameTurn>();
			// Loop over deck turns
			for (var i:int = 0; i < gameTurns.turns.length; i++)
			{
				// Add the card to the list
				turns.push(gameTurns.turns[i].clone());
			}
			// Clear current turns			
			clear();
			// Popolate array with temp turns
			for (var j:int = 0; j < turns.length; j++)
			{
				_turns.push(turns[j]);
			}
		}
		
		public function clone():GameTurns
		{
			var GameTurns:GameTurns = new GameTurns();
			GameTurns.copy(this);
			return GameTurns;
		}
		*/
		
		public function getLastTurn():GameTurn {
			return _turns[_turns.length - 1];
		}
		
		public function add(gameTurn:GameTurn):void
		{
			// LT_TODO: verifica esistenza turno
			_turns.push(gameTurn);
		}
		
		override public function clear():void
		{
			_turns.splice(0, _turns.length);
		}
	}
}